const express = require("express");
const apiRouter = express.Router();
const fs = require("fs");
const writeData = require("../../../db/module/writeData");
const readData = require("../../../db/module/readData");
const delData = require("../../../db/module/delData");

// 文件上传中间件

// 获取网站列表
apiRouter.post("/websiteList", async (req, res) => {
  // 获取到客户端通过查询字符串发送到服务器的数据
  try {
    const params = req.body;

    let result = await readData(params, "websiteList");

    res.send(result);
  } catch (error) {
    res.send({
      code: 500,
      msg: error,
    });
  }
});

// 新增文件
apiRouter.post("/addWebsite", async (req, res) => {
  try {
    const params = req.body;
    params.createTime = new Date().toLocaleString();
    let result = await writeData(params, "websiteList");
    res.send(result);
  } catch (error) {
    res.send({
      code: 50000,
      msg: error,
    });
  }
});

// 删除文件
apiRouter.post("/delWebsite", async (req, res) => {
  try {
    const params = req.body;

    let result = await delData(params, "websiteList");

    res.send(result);
  } catch (error) {
    res.send({
      code: 50000,
      msg: error,
    });
  }
});

module.exports = apiRouter;
